package fr.alesia.deepstack.io.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import fr.alesia.deepstack.bean.Tournament;
import fr.alesia.deepstack.factory.BeanFactory;
import fr.alesia.deepstack.io.BeanReader;

public class TournamentReader implements BeanReader<Tournament> {

	private static BeanReader<Tournament> instance = null;
	private static SimpleDateFormat formatter = null;
	
	private TournamentReader(){
		
		formatter = new SimpleDateFormat();
		formatter.applyPattern("dd/MM/yyyy");
	}
	
	public static BeanReader<Tournament> getInstance(){
		if(instance == null){
			instance = new TournamentReader();
		}
		return instance;
	}
	
	public Tournament read(String line) throws ParseException  {
		
		String[] tokens = line.split(",");
		if(tokens.length == 2){
			
			String id = "k" + tokens[0];
			Date date = formatter.parse(tokens[1]);
			Tournament tournament = BeanFactory.createTournament(date);
			tournament.setId(id);
			return tournament;
		}
		else{
			return null;
		}
	}
}
